package GUI.lecturerGUI;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextArea;

import GUI.MainGUI;
import controllers.LecturerController;

import entities.*;

import javax.swing.JSlider;
import javax.swing.JComboBox;

/**
 * 
 * Lecturer Duration Time Request
 * 
 */
public class LecturerDurationTimeRequest extends JPanel implements
		ActionListener, ChangeListener, KeyListener, FocusListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtEnterTimeMin;
	private JComboBox cboxExamCode;
	private JButton btnMainMenu;
	private JButton btnExit;
	private JLabel lblEnterExamCode;
	private JButton btnSendRequest;
	private JLabel lblTimeDuration;
	private JSlider sldTimeDuration;
	private JLabel lblEnterReasons;
	private JTextArea txtrRequestReasons;
	private List<ExtractedExam> examLst;
	private LecturerController lecturerCtrl;

	/**
	 * the Lecturer request for duration time extension
	 * 
	 * @param lecturerCtrl
	 *            the lecturer controller
	 */
	public LecturerDurationTimeRequest(LecturerController lecturerCtrl) {
		super();
		this.lecturerCtrl = lecturerCtrl;
		initialize();
	}

	/**
	 * initializing the GUI
	 */
	private void initialize() {
		setBorder(new TitledBorder(null, "Lecturer Duration Time Request: ",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(0, 0, MainGUI.setX, MainGUI.setY);
		setLayout(null);

		add(GETcboxExamCode());
		add(GETbtnMainMenu());
		add(GETbtnExit());
		add(GETbtnSendRequest());
		add(GETlblEnterExamCode());
		add(GETlblEnterTime());
		add(GETlblEnterReasons());
		add(GETtxtEnterTimeIn());
		add(GETtxtrRequestReasons());
		add(GETsldEnterTime());
	}

	private JComboBox GETcboxExamCode() {
		if (cboxExamCode == null) {
			cboxExamCode = new JComboBox();
			cboxExamCode.setMaximumRowCount(50);
			cboxExamCode.setBounds(190, 63, 200, 20);
		}
		return cboxExamCode;
	}

	private JSlider GETsldEnterTime() {
		if (sldTimeDuration == null) {
			sldTimeDuration = new JSlider();
			sldTimeDuration.setMajorTickSpacing(10);
			sldTimeDuration.setPaintTicks(true);
			sldTimeDuration.setValue(0);
			sldTimeDuration.setMinorTickSpacing(1);
			sldTimeDuration.setMaximum(90);
			sldTimeDuration.setBounds(190, 116, 200, 23);
			sldTimeDuration.addChangeListener(this);
		}
		return sldTimeDuration;
	}

	private JButton GETbtnMainMenu() {
		if (btnMainMenu == null) {
			btnMainMenu = new JButton("Main menu");
			btnMainMenu.setBounds(21, 443, 111, 34);
			btnMainMenu.addActionListener(this);
		}
		return btnMainMenu;
	}

	private JButton GETbtnExit() {
		if (btnExit == null) {
			btnExit = new JButton("EXIT");
			btnExit.setBounds(373, 443, 117, 34);
			btnExit.addActionListener(this);
		}
		return btnExit;
	}

	private JLabel GETlblEnterExamCode() {
		if (lblEnterExamCode == null) {
			lblEnterExamCode = new JLabel("Enter exam code:");
			lblEnterExamCode.setBounds(24, 63, 156, 20);
		}
		return lblEnterExamCode;
	}

	private JButton GETbtnSendRequest() {
		if (btnSendRequest == null) {
			btnSendRequest = new JButton("Send request");
			btnSendRequest.setFont(new Font("Plantagenet Cherokee", Font.PLAIN,
					14));
			btnSendRequest.setBounds(238, 356, 128, 34);
			btnSendRequest.addActionListener(this);
			btnSendRequest.setEnabled(false);
		}
		return btnSendRequest;
	}

	private JLabel GETlblEnterTime() {
		if (lblTimeDuration == null) {
			lblTimeDuration = new JLabel("Time duration:");
			lblTimeDuration.setBounds(21, 116, 150, 25);
		}
		return lblTimeDuration;
	}

	private JTextField GETtxtEnterTimeIn() {
		if (txtEnterTimeMin == null) {
			txtEnterTimeMin = new JTextField();
			txtEnterTimeMin.setEnabled(false);
			txtEnterTimeMin.setBounds(396, 116, 35, 25);
			txtEnterTimeMin.setColumns(10);
			txtEnterTimeMin.setText("0");
		}
		return txtEnterTimeMin;

	}

	private JLabel GETlblEnterReasons() {
		if (lblEnterReasons == null) {
			lblEnterReasons = new JLabel("Enter reasons for the request:");
			lblEnterReasons.setBounds(21, 162, 256, 25);
		}
		return lblEnterReasons;
	}

	private JTextArea GETtxtrRequestReasons() {
		if (txtrRequestReasons == null) {
			txtrRequestReasons = new JTextArea();
			txtrRequestReasons.setText("Request reasons");
			txtrRequestReasons.setBounds(10, 198, 380, 117);
			txtrRequestReasons.addKeyListener(this);
			txtrRequestReasons.addFocusListener(this);
		}
		return txtrRequestReasons;
	}

	/**
	 * 
	 * @return the selected exam code
	 */
	public String getCode() {
		return examLst.get(cboxExamCode.getSelectedIndex()).getExamCode();
	}

	/**
	 * 
	 * @return the desired duration time
	 */
	public long getTime() {
		return Long.parseLong(txtEnterTimeMin.getText())*60*1000;
	}

	/**
	 * 
	 * @return the reasons for the duration time request
	 */
	public String getReasons() {
		return txtrRequestReasons.getText();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnMainMenu) {
			cleanExams();
			lecturerCtrl.handleLectutereGUI(LecturerController.MAIN);
		}
		if (e.getSource() == btnExit) {
			cleanExams();
			lecturerCtrl.handleLectutereGUI(LecturerController.EXIT);
		}
		if (e.getSource() == btnSendRequest) {
			lecturerCtrl.addDurationTimeRequest();
		}
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if (e.getSource() == sldTimeDuration) {
			Integer val = new Integer(sldTimeDuration.getValue());
			txtEnterTimeMin.setText(val.toString());
			if (val == 0)
				btnSendRequest.setEnabled(false);
			else if (examLst != null
					&& !txtrRequestReasons.getText().equals("")
					&& !txtrRequestReasons.getText().equals("Request reasons"))
				btnSendRequest.setEnabled(true);
		}

	}

	/**
	 * cleaning components for the next use of the GUI
	 */
	public void cleanExams() {
		if (examLst != null) {
			sldTimeDuration.setValue(0);
			txtEnterTimeMin.setText("0");
			txtrRequestReasons.setText("Request reasons");
			examLst.clear();
			cboxExamCode.removeAllItems();
			btnSendRequest.setEnabled(false);
			cboxExamCode.setEnabled(false);
		}
	}

	/**
	 * removing exam from the exam list & combo box
	 */
	public void removeExam() {
		examLst.remove(cboxExamCode.getSelectedIndex());
		cboxExamCode.removeItemAt(cboxExamCode.getSelectedIndex());
		if (examLst.size() == 0) {
			cboxExamCode.setEnabled(false);
			btnSendRequest.setEnabled(false);
			sldTimeDuration.setValue(0);
			txtEnterTimeMin.setText("0");
			txtrRequestReasons.setText("Request reasons");
		}
	}

	/**
	 * adding exams to the combo box
	 * 
	 * @param activeExams
	 *            the exams to be added
	 */
	public void addActiveExams(ArrayList<ExtractedExam> activeExams) {
		this.examLst = new ArrayList<ExtractedExam>();
		this.examLst.addAll(activeExams);

		for (int i = 0; i < (activeExams.size()); i++)
			cboxExamCode.addItem(examLst.get(i).getExamCode() + " : "
					+ examLst.get(i).getSubjectID()
					+ examLst.get(i).getCourseID() + examLst.get(i).getExamID()
					+ " : " + examLst.get(i).getExamExtractDate());
		if (examLst.size() == 0)
			cboxExamCode.setEnabled(false);
		else
			cboxExamCode.setEnabled(true);
		cboxExamCode.addActionListener(this);

	}

	@Override
	public void keyPressed(KeyEvent e) {

		if (e.getSource() == txtrRequestReasons
				&& (!txtEnterTimeMin.getText().equals("0"))
				&& examLst.size() > 0
				&& !txtrRequestReasons.getText().equals("")
				&& !txtrRequestReasons.getText().equals("Request reasons"))
			btnSendRequest.setEnabled(true);
	}

	@Override
	public void keyReleased(KeyEvent arg0) {

	}

	@Override
	public void keyTyped(KeyEvent arg0) {

	}

	@Override
	public void focusGained(FocusEvent e) {
		if (e.getSource() == txtrRequestReasons
				&& txtrRequestReasons.getText().equals("Request reasons"))
			txtrRequestReasons.setText("");
	}

	@Override
	public void focusLost(FocusEvent e) {
		if (e.getSource() == txtrRequestReasons
				&& txtrRequestReasons.getText().equals(""))
			txtrRequestReasons.setText("Request reasons");

	}
}
